package com.example.rotateimageview.revealeffect

import android.animation.Animator
import android.os.Bundle
import android.view.View
import android.view.ViewAnimationUtils
import androidx.appcompat.app.AppCompatActivity
import com.example.rotateimageview.R
import kotlinx.android.synthetic.main.layout_reveal_effect_.*


/**
 * 揭露动画
 */
class RevealEffectActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_reveal_effect_)

        fl_btn.setOnClickListener {
            val vLocation = IntArray(2)
            fl_btn.getLocationInWindow(vLocation)
            val centerX: Int = vLocation[0] + fl_btn.getMeasuredWidth() / 2
            val centerY: Int = vLocation[1] + fl_btn.getMeasuredHeight() / 2

            val height: Int = view_puppet.getHeight()
            val width: Int = view_puppet.getWidth()
            val maxRradius = Math.hypot(height.toDouble(), width.toDouble()).toFloat()
            val animator =
                ViewAnimationUtils.createCircularReveal(view_puppet, centerX, centerY, 0f, maxRradius)
            animator.duration = 1000
            animator.addListener(object : Animator.AnimatorListener {
                override fun onAnimationStart(animation: Animator) {
                    view_puppet.setVisibility(View.VISIBLE)
                }

                override fun onAnimationEnd(animation: Animator) {
                    view_puppet.setVisibility(View.GONE)
                }
                override fun onAnimationCancel(animation: Animator) {

                }
                override fun onAnimationRepeat(animation: Animator) {

                }
            })
            animator.start()
        }

    }
}